package com.devplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devplatform.mapper.FansMapper;
import com.devplatform.mapper.article.ArticleMapper;
import com.devplatform.mapper.user.UserMapper;
import com.devplatform.pojo.Fans;
import com.devplatform.pojo.article.Article;
import com.devplatform.pojo.dto.ChangePasswordDto;
import com.devplatform.pojo.user.User;
import com.devplatform.service.user.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {
    private final UserMapper userMapper;
    private final BCryptPasswordEncoder passwordEncoder;
    private final FansMapper fansMapper;
    @Value("${defaultAvatar}")
    private String defaultAvatar;
    private final ArticleMapper articleMapper;

    @Override
    public boolean register(User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        if (user.getAvatar() == null) {
            user.setAvatar(defaultAvatar);
        }
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean changePassword(Long userId, ChangePasswordDto changePasswordDto) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }
        //验证原来密码
        if (!passwordEncoder.matches(changePasswordDto.getOldPassword(), user.getPassword())) {
            return false;
        }
        //判断新的密码是否一致
        if (!changePasswordDto.getNewPassword().equals(changePasswordDto.getConfirmPassword())) {
            return false;
        }
        //一致，就更新密码
        user.setPassword(passwordEncoder.encode(changePasswordDto.getNewPassword()));
        return userMapper.updateById(user) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long userId, String password, String reason) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }
        //验证密码
        if (!passwordEncoder.matches(password, user.getPassword())) {
            return false;
        }
        //删除名下文章
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getAuthorId, userId);
        articleMapper.delete(articleWrapper);
        //删除关联粉丝与关注
        LambdaQueryWrapper<Fans> fansWrapper = new LambdaQueryWrapper<>();
        fansWrapper.eq(Fans::getAuthorId, userId).or().eq(Fans::getFansId, userId);
        fansMapper.delete(fansWrapper);

        return userMapper.deleteById(user) > 0;
    }

    @Override
    public User getUserInfo(Long userId) {
        return userMapper.selectById(userId);
    }


}
